package com.ez.supervise.action.biz;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.struts2.ServletActionContext;

import com.ez.supervise.action.base.BaseJsonAction;
import com.ez.supervise.core.MyUtils;
import com.ez.supervise.core.Page;
import com.ez.supervise.core.Result;
import com.ez.supervise.model.biz.DemocracyCheckman;
import com.ez.supervise.service.DemocracyCheckmanService;
import com.ez.supervise.util.JsonUtils;

public class DemocracyCheckmanAction extends BaseJsonAction {
    private static final long serialVersionUID = 8735170970520475073L;
    private Long id;
    private DemocracyCheckmanService democracyCheckmanService;
    private DemocracyCheckman democracyCheckman;
    private Page page;

    public String saveDemocracyCheckman() {
	try {
	    if (democracyCheckman.isNew()) {
		democracyCheckmanService.insert(democracyCheckman);
	    } else {
		democracyCheckmanService.update(democracyCheckman);
	    }
	    Result result = new Result(democracyCheckman.getId());
	    result.setEntity(democracyCheckman);
	    JsonConfig config = new JsonConfig();
	    config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new",
		"content" });
	    outString(JSONObject.fromObject(result, config).toString());
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAllDemocracyCheckman() {
	try {
	    page = new Page();
	    List result = democracyCheckmanService.findAll();
	    page.setRoot(result);
	    page.setTotalProperty(result.size());
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }
    
    public String findDemocracyCheckmanByPage() {
	try {
	    page = new Page();
	    HttpServletRequest request = getRequest();
	    page.putCondition("year", NumberUtils.isNumber(request.getParameter("year")) ? Long.valueOf(request.getParameter("year")) : null);
	    int start = NumberUtils.toInt(request.getParameter("start"), 0);
	    int limit = NumberUtils.toInt(request.getParameter("limit"), 20);
	    page.setStart(start++);
	    page.setLimit(limit);
	    page.addPageInfo(); 
	    democracyCheckmanService.findDemocracyCheckmanByPage(page);
	    outJsonString(JsonUtils.fromObject(page, new String[] { "creator", "createTime", ",modifier", "modifyTime",
		"isDeleted", "new" }));
	    
	} catch (Exception e) {
	    e.printStackTrace();
	    outString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    public String findAll() {
	page = new Page();
	page.setTotalProperty(0);
	page.setRoot(democracyCheckmanService.findAll());

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(page, config);
	// System.out.println(jsonObj.toString());
	outJsonString(jsonObj.toString());
	return SUCCESS;
    }

    /**
     * @return
     */
    public String deleteDemocracyCheckman() {
	try {
	    if (id != null && id > 0) {
		democracyCheckmanService.delete(id);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}
	return SUCCESS;
    }

    /**
     * @return
     * @throws Exception
     */
    public String updateDemocracyCheckman() throws Exception {
	String fieldName = getRequest().getParameter("fieldName");
	String fieldValue = getRequest().getParameter("fieldValue");
	String strDemocracyCheckmanId = getRequest().getParameter("id");
	try {
	    if (id != null && id > 0) {
		DemocracyCheckman saveDemocracyCheckman = new DemocracyCheckman();
		saveDemocracyCheckman.setId(id);
		BeanUtils.setProperty(saveDemocracyCheckman, fieldName, fieldValue);
		democracyCheckmanService.update(saveDemocracyCheckman);
	    }
	    outJsonString(RESULT_SUCCESS);
	} catch (Exception e) {
	    outJsonString(RESULT_FAIL);
	}

	return SUCCESS;
    }

    private String targetDirectory() {
	return ServletActionContext.getServletContext().getRealPath("/" + UPLOAD_FILE_PATH);
    }

    private String generateFileName(String fileName) {
	DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
	String formatDate = format.format(new Date());

	int random = new Random().nextInt(10000);
	if (StringUtils.isNotBlank(fileName)) {
	    int position = fileName.lastIndexOf(".");
	    String extension = fileName.substring(position);
	    return formatDate + random + extension;
	} else {
	    return formatDate + random;
	}

    }

    public Long getId() {
	return id;
    }

    public void setId(Long id) {
	this.id = id;
    }

    public static void main(String[] args) {
	DemocracyCheckman democracyCheckman = new DemocracyCheckman();
	democracyCheckman.setId(1L);
	// democracyCheckman.setName("linlinyu");
	JSONObject json = JSONObject.fromObject(democracyCheckman);

	System.out.println(json);

	JsonConfig config = new JsonConfig();
	config.setExcludes(new String[] { "creator", "createTime", ",modifier", "modifyTime", "isDeleted", "new", });

	JSONObject jsonObj = JSONObject.fromObject(democracyCheckman, config);
	System.out.println(jsonObj.toString());
    }

    public void setDemocracyCheckmanService(DemocracyCheckmanService democracyCheckmanService) {
	this.democracyCheckmanService = democracyCheckmanService;
    }

    public DemocracyCheckman getDemocracyCheckman() {
	return democracyCheckman;
    }

    public void setDemocracyCheckman(DemocracyCheckman democracyCheckman) {
	this.democracyCheckman = democracyCheckman;
    }

    public Page getPage() {
	return page;
    }

    public void setPage(Page page) {
	this.page = page;
    }

}
